package com.doc.desensitization.common.utils;

/**
 * 脱敏工具类，实现具体的脱敏逻辑
 */
public class DesensitizationUtil {

    /**
     * 根据类型进行脱敏
     */
    public static String desensitize(String value, DesensitizationType type, int prefixLength, int suffixLength) {
        if (value == null || value.isEmpty()) {
            return value;
        }
        
        switch (type) {
            case PHONE:
                return desensitizePhone(value);
            case EMAIL:
                return desensitizeEmail(value);
            case ID_CARD:
                return desensitizeIdCard(value);
            case ADDRESS:
                return desensitizeAddress(value);
            case BANK_CARD:
                return desensitizeBankCard(value);
            case CUSTOM:
                return desensitizeCustom(value, prefixLength, suffixLength);
            default:
                return value;
        }
    }
    
    /**
     * 手机号脱敏：保留前3位和后4位，中间用*代替
     * 例如：13812345678 -> 138****5678
     */
    private static String desensitizePhone(String phone) {
        if (phone.length() != 11) {
            return phone;
        }
        return phone.replaceAll("(\\d{3})\\d{4}(\\d{4})", "$1****$2");
    }
    
    /**
     * 邮箱脱敏：保留前1位和域名，中间用*代替
     * 例如：zhangsan@example.com -> z****@example.com
     */
    private static String desensitizeEmail(String email) {
        int index = email.indexOf("@");
        if (index <= 1) {
            return email;
        }
        return email.substring(0, 1) + "****" + email.substring(index);
    }
    
    /**
     * 身份证号脱敏：保留前6位和后4位，中间用*代替
     * 例如：110101199001011234 -> 110101********1234
     */
    private static String desensitizeIdCard(String idCard) {
        if (idCard.length() != 18) {
            return idCard;
        }
        return idCard.replaceAll("(\\d{6})\\d{8}(\\d{4})", "$1********$2");
    }
    
    /**
     * 地址脱敏：保留前6位，后面用*代替
     * 例如：北京市朝阳区某某街道123号 -> 北京市朝***
     */
    private static String desensitizeAddress(String address) {
        if (address.length() <= 6) {
            return address;
        }
        return address.substring(0, 6) + "***";
    }
    
    /**
     * 银行卡号脱敏：保留前4位和后4位，中间用*代替
     * 例如：6222021234567890123 -> 6222*******90123
     */
    private static String desensitizeBankCard(String bankCard) {
        if (bankCard.length() <= 8) {
            return bankCard;
        }
        return bankCard.substring(0, 4) + "*******" + bankCard.substring(bankCard.length() - 4);
    }
    
    /**
     * 自定义脱敏：保留指定长度的前缀和后缀，中间用*代替
     */
    private static String desensitizeCustom(String value, int prefixLength, int suffixLength) {
        if (value.length() <= prefixLength + suffixLength) {
            return value;
        }
        
        StringBuilder result = new StringBuilder();
        // 添加前缀
        result.append(value.substring(0, prefixLength));
        // 添加*
        int starCount = value.length() - prefixLength - suffixLength;
        for (int i = 0; i < starCount; i++) {
            result.append("*");
        }
        // 添加后缀
        result.append(value.substring(value.length() - suffixLength));
        
        return result.toString();
    }
}
